#include "stdafx.h"
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <functional>
#include "DebugUtils.h"

using namespace std;

static FILE* fp = NULL;

void openLog() {
#ifdef DEBUG_SAVE_LOG
    hash<clock_t> HashFunc;
    char* fname = format("_log_%x.txt", HashFunc(clock()));
    fp = fopen(fname, "w");
    delete[] fname;
#endif
}

void closeLog() {
#ifdef DEBUG_SAVE_LOG
    if (fp == NULL) {
        return;
    }
    fclose(fp);
    fp = NULL;
#endif
}

char* format(char* fmt, ...) {
    va_list list;
    va_start(list, fmt);
    int size = snprintf(NULL, 0, fmt, list) + 1;
    char* p = new char[size];
    snprintf(p, size, fmt, list);
    va_end(list);
    return p;
}
void writeLog(char* fmt, ...) {
#ifdef DEBUG_SAVE_LOG
    if (fp == NULL) {
        return;
    }
    va_list list;
    va_start(list, fmt);
    vfprintf(fp, fmt, list);
    va_end(list);
    fflush(fp);
#endif
}